package com.wego.pdcms.module.main.controller.admin.patient;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.wego.pdcms.framework.common.pojo.PageResult;
import com.wego.pdcms.framework.common.pojo.CommonResult;
import static com.wego.pdcms.framework.common.pojo.CommonResult.success;

import com.wego.pdcms.framework.excel.core.util.ExcelUtils;

import com.wego.pdcms.framework.operatelog.core.annotations.OperateLog;
import static com.wego.pdcms.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.wego.pdcms.module.main.controller.admin.patient.vo.*;
import com.wego.pdcms.module.main.dal.dataobject.patient.PatientDO;
import com.wego.pdcms.module.main.convert.patient.PatientConvert;
import com.wego.pdcms.module.main.service.patient.PatientService;

@Api(tags = "管理后台 - 患者信息")
@RestController
@RequestMapping("/main/patient")
@Validated
public class PatientController {

    @Resource
    private PatientService patientService;

    @PostMapping("/create")
    @ApiOperation("创建患者信息")
    @PreAuthorize("@ss.hasPermission('main:patient:create')")
    public CommonResult<Integer> createPatient(@Valid @RequestBody PatientCreateReqVO createReqVO) {
        return success(patientService.createPatient(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新患者信息")
    @PreAuthorize("@ss.hasPermission('main:patient:update')")
    public CommonResult<Boolean> updatePatient(@Valid @RequestBody PatientUpdateReqVO updateReqVO) {
        patientService.updatePatient(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除患者信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('main:patient:delete')")
    public CommonResult<Boolean> deletePatient(@RequestParam("id") Integer id) {
        patientService.deletePatient(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得患者信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('main:patient:query')")
    public CommonResult<PatientRespVO> getPatient(@RequestParam("id") Integer id) {
        PatientDO patient = patientService.getPatient(id);
        return success(PatientConvert.INSTANCE.convert(patient));
    }

    @GetMapping("/list")
    @ApiOperation("获得患者信息列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('main:patient:query')")
    public CommonResult<List<PatientRespVO>> getPatientList(@RequestParam("ids") Collection<Integer> ids) {
        List<PatientDO> list = patientService.getPatientList(ids);
        return success(PatientConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得患者信息分页")
    @PreAuthorize("@ss.hasPermission('main:patient:query')")
    public CommonResult<PageResult<PatientRespVO>> getPatientPage(@Valid PatientPageReqVO pageVO) {
        PageResult<PatientDO> pageResult = patientService.getPatientPage(pageVO);
        return success(PatientConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出患者信息 Excel")
    @PreAuthorize("@ss.hasPermission('main:patient:export')")
    @OperateLog(type = EXPORT)
    public void exportPatientExcel(@Valid PatientExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<PatientDO> list = patientService.getPatientList(exportReqVO);
        // 导出 Excel
        List<PatientExcelVO> datas = PatientConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "患者信息.xls", "数据", PatientExcelVO.class, datas);
    }

}
